package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.BulletinReceiveLogic;
import cn.topevery.garden.entity.dto.BulletinReceiveDTO;
import cn.topevery.garden.entity.query.BulletinReceiveQuery;
import cn.topevery.garden.entity.vo.BulletinReceiveVO;
import cn.topevery.garden.entity.po.BulletinReceive;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.dozermapper.core.Mapper;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 公告接收对象
 */
@Api(tags = "公告接收对象")
@RestController
@RequestMapping("/bulletinReceive")
public class BulletinReceiveController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    BulletinReceiveLogic bulletinReceiveLogic;


    /**
     * 获取公告接收对象分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取公告接收对象页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<BulletinReceiveVO>> listPage(@RequestBody BulletinReceiveQuery query) {
        return JsonResult.ok(bulletinReceiveLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 公告接收对象信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody BulletinReceiveDTO dto) {
        bulletinReceiveLogic.saveOrUpdate(getUser().id.toString(),mapper.map(dto, BulletinReceive.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<BulletinReceiveVO> getInfoById(@RequestParam Integer id) {
        BulletinReceive entity = bulletinReceiveLogic.getEntity(id);
        BulletinReceiveVO vo= mapper.map(entity, BulletinReceiveVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        bulletinReceiveLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出公告接收对象信息")
    @GetMapping("/export")
    public void export(BulletinReceiveQuery query, HttpServletResponse response) {
        List<BulletinReceiveVO> list = bulletinReceiveLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "公告接收对象", "sheet1", BulletinReceiveVO.class, "公告接收对象", response);
    }

}
